package models
{
    import com.adobe.serialization.json.JSON;
    import com.pamakids.manager.LoadManager;
    
    import service.SOService;
    
    public class ThemeManager
    {
        
        
        public var prevTheme:ThemeVo
        
        public var prevThemeFolder:ThemeFolderVo
        
        private var loadedCB:Function;
        
        public function initialize(cb:Function):void
        {
            loadedCB=cb;
            LoadManager.instance.loadText('assets/themes/local.json',loadComplete);
            CourseManager.getInstance().initialize();
        }
        
        private function loadComplete(s:String):void
        {
            var o:Object=com.adobe.serialization.json.JSON.decode(s);
            parse(o);
        }
        
        private function parse(o:Object):void
        {
            mThemeList=[];
            
            var arr:Array=o.themepacks;
            for each (var theme:Object in arr) 
            {
                var folder:ThemeFolderVo=new ThemeFolderVo(theme.path,theme.num);
                mThemeList.push(folder);
            }
            
            if(loadedCB!=null)
                loadedCB();
            loadedCB=null;
        }
        
        public function getThemeList():Array
        {
            return mThemeList;
        }
        
        public function getThemeDirByType(type:String):ThemeFolderVo
        {
            var arr:Array=getThemeList();
            for (var i:int = 0; i < arr.length; i++) 
            {
                var dir:Object=arr[i];
                if(dir is ThemeFolderVo)
                {
                    if (dir.path.indexOf(type)>=0)
                    {
                        return dir as ThemeFolderVo
                    }
                }
            }
            
            return CourseManager.getInstance().getThemeDirByType(type);
            
//			for each (var dir:ThemeFolderVo in arr)
//			{
//				if (dir.path.indexOf(type)>=0)
//				{
//					return dir
//				}
//			}
            return null
        }
        
        
        private var mThemeList:Array=[]
        
        private static var mInstance:ThemeManager
        
        public static function getInstance():ThemeManager
        {
            return mInstance||=new ThemeManager
        }
        
        public function getThemeVoByPath(path:String):ThemeVo
        {
            if(!path)
                path='blank/0';
            
            if(path.indexOf('album')>=0)
                return new ThemeVoPractice(path);
            
            var theme:String=path;
            var vo:ThemeVo;
            var arr:Array=theme.split("/");
            if(arr.length==2)
            {
                var pack:String=arr[0];
                var index:int=int(arr[1]);
                var folder:ThemeFolderVo=getThemeDirByType(pack);
                if(folder)
                {
                    vo=folder.themeList[index]
                    if(vo)
                        return vo;
                }
            }
            return getThemeVoNew(path);
        }
        
        public function getThemeVoNew(path:String):ThemeVo
        {
            return new ThemeVoNew(path);
        }
    }
}


